package cn.kgc.service.impl;

import cn.kgc.entity.AppCategory;
import cn.kgc.mapper.AppCategoryMapper;
import cn.kgc.service.AppCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class AppCategoryServiceImpl implements AppCategoryService {
    @Autowired
    AppCategoryMapper appCategoryMapper;

    @Cacheable(cacheNames = "app_category",key = "'child_'+#parentId")
    public List<AppCategory> findChildCategory(Integer parentId) {
        Example example = new Example(AppCategory.class);
        if (parentId == null) {
            example.and().andIsNull("parentId");
        }else{
            example.and().andEqualTo("parentId",parentId);
        }
        return appCategoryMapper.selectByExample(example);
    }

    @Cacheable(cacheNames = "app_category",key = "#id")
    public AppCategory findCategoryById(Integer id) {
        return appCategoryMapper.selectByPrimaryKey(id);
    }
}
